package com.tigger.core.schedule;

import com.tigger.core.po.EsFilePo;
import com.tigger.core.redis.RedisServer;
import com.tigger.core.redisson.lock.RedissonRLock;
import com.tigger.core.service.IEsFileBiz;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Created by root on 2018/7/21.
 */
@Component
public class SpringSchedule {

    private static Logger LOGGER = LoggerFactory.getLogger(SpringSchedule.class);

    @Autowired
    private IEsFileBiz esFileBiz;
    @Autowired
    private RedisServer redisServer;

    @Scheduled(cron = "0/10 * * * * ?")
    public void test() {
        LOGGER.info("spring scheduling");
        try {
            boolean isLocked = RedissonRLock.lock("lock01", 500);
            LOGGER.info("isLocked: {}.", isLocked);
            EsFilePo file = esFileBiz.findById("7ada6fa49ce941c687103bc705a0c559");
            redisServer.set("7ada6fa49ce941c687103bc705a0c559", "value");
            String value = (String) redisServer.get("7ada6fa49ce941c687103bc705a0c559");
            LOGGER.info("redis cache value: {}.", value);
        } finally {
            LOGGER.info("release lock.");
            RedissonRLock.unlock("lock01");
        }

    }
}
